<?php
/**
 * lemocms
 * ============================================================================
 * 版权所有 2018-2027 lemocms，并保留所有权利。
 * 网站地址: https://www.lemocms.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp6实现
 * ============================================================================
 * Author: yuege
 * Date: 2019/8/2
 */

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\StudentTopic;
use app\common\traits\Curd;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
use app\common\model\Topic as TopicModel;
use app\common\model\Schedule;

class Topic extends Backend
{
    use Curd;

    protected $user_info;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->modelClass = new TopicModel();
        $this->user_info = session('admin');
    }

    /**
     * 老师
     * 渲染页面
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $keys = $this->request->post('keys', '', 'trim');

            $where = [];
            if ($keys != '') {
                $where[] = ['title', 'like', '%' . $keys . '%'];
            }

            $page = $this->request->post('page');
            $limit = $this->request->post('limit');
            $tol = ($page - 1) * $limit;

            $banner = TopicModel::where($where)->limit($tol, $limit)->select();
            $count = TopicModel::where($where)->count();

            if (!$banner) error();
            success($banner, $count);
        }
        return view();
    }

    /**
     * 学生
     * 选择题目，页面渲染
     * 作者：杨聪
     * 时间：2021/3/10 17:22
     */
    public function student_select()
    {
        if ($this->request->isPost()) {
            $keys = $this->request->post('keys', '', 'trim');

            $where = [];
            if ($keys != '') {
                $where[] = ['title', 'like', '%' . $keys . '%'];
            }

            $page = $this->request->post('page');
            $limit = $this->request->post('limit');
            $tol = ($page - 1) * $limit;

            $banner = TopicModel::where($where)->limit($tol, $limit)->select();
            $count = TopicModel::where($where)->count();

            if (!$banner) error();
            success($banner, $count);
        }

        // 查询是否已经选择题目
        $student_topic = StudentTopic::where(['student_id' => $this->user_info['id']])->find();
        if (!$student_topic){
            $status = 0;
        } else{
            $status = 1;
        }

        $view = [
            'status' => $status
        ];

        return view('', $view);
    }

    /**
     * 学生
     * 选择题目
     * 作者：杨聪
     * 时间：2021/3/10 17:58
     */
    public function select_topic()
    {

        if ($this->request->isPost()) {
            $param = \request()->post();

            // 启动事务
            Db::startTrans();
            try {
                // 查询是否已经选择题目
                $student_topic = StudentTopic::create([
                    'student_id' => $this->user_info['id'],
                    'topic_id' => $param['id']
                ]);
                if (!$student_topic) $this->error('失败');

                // 创建四个步骤
                $data = array(
                    [
                        'student_id' => $this->user_info['id'],
                        'title' => '需求分析环节'
                    ],
                    [
                        'student_id' => $this->user_info['id'],
                        'title' => '课程资源分析'
                    ],
                    [
                        'student_id' => $this->user_info['id'],
                        'title' => '学习目标，内容设计'
                    ],
                    [
                        'student_id' => $this->user_info['id'],
                        'title' => '课程实践效果分析'
                    ]
                );

                $scheduld = new Schedule();
                if (!$scheduld->saveAll($data)) $this->error('失败');

                // 提交事务
                Db::commit();
                retMsg(1,'选择成功');

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }

        }
    }


}